<?php
header("content-type:text/html;charset=utf-8");
if (!empty($_POST['webname'])) {
	echo "<hr>";
	echo "****************************************--开始--******************************************";
	echo "<hr>";
	echo "<span style='color: yellowgreen;'>B级别</span>
		<span style='color: green;'>KB级别</span>
		<span style='color: blueviolet;'>MB级别</span>
		<span style='color: red;'>GB级别</span>";
	echo "<hr>";
}
// 单位自动转换函数 
function getRealSize($size) {
	$kb = 1024;// Kilobyte  
	$mb = 1024 * $kb; // Megabyte  
	$gb = 1024 * $mb; // Gigabyte  
	$tb = 1024 * $gb; // Terabyte  
	if($size < $kb){
		return $size." B";  
	}else if($size < $mb)  {
		return round($size/$kb,2)." KB";  
	}else if($size < $gb)  {
		return round($size/$mb,2)." MB"; 
	}else if($size < $tb){  
	    return round($size/$gb,2)." GB";  
	}else{
		return round($size/$tb,2)." TB";  
	} 
}
$dir = 0;
$files = 0;
$arrb =array();
$arrkb =array();
$arrmb =array();
$arrgb =array();

//读取所有
function getdir($path,$bsize,$kbsize,$mbsize,$gbsize){
	global $dir,$files,$arrb,$arrkb,$arrmb,$arrgb;
	$arr=scandir($path);
	foreach($arr as $v){
		if($v!="."&&$v!=".."){
			if(is_dir($path."/".$v)){
				$dir++;
				echo "目录：".$path."/".$v;
				echo "<br>";
				getdir($path."/".$v,$bsize,$kbsize,$mbsize,$gbsize);
			}else{
				$files++;
				echo "文件：".$path."/".$v;
				$fsize=getRealSize(filesize("$path/$v"));
				$ext = substr($fsize, (strrpos($fsize, " ") + 1));
				$num = substr($fsize,0,strrpos($fsize, " "));
				if($ext=="B"){
					if($num>$bsize){
						$arrb[]=$path."/".$v."---<span style='color: yellowgreen;'>".$fsize."</span>";
						echo "---<span style='font-size: 48px; color: yellowgreen;'>".$fsize."</span>";
					}else{
						echo "---<span style='color: yellowgreen;'>".$fsize."</span>";
					}
				}elseif($ext=="KB"){
					if($num>$kbsize){
						$arrkb[]=$path."/".$v."---<span style='color: green;'>".$fsize."</span>";
						echo "---<span style='font-size: 48px; color: green;'>".$fsize."</span>";
					}else{
						echo "---<span style='color: green;'>".$fsize."</span>";
					}
				}elseif($ext=="MB"){
					if($num>$mbsize){
						$arrmb[]=$path."/".$v."---<span style='color: blueviolet;'>".$fsize."</span>";
						echo "---<span style='font-size: 48px; color: blueviolet;'>".$fsize."</span>";
					}else{
						echo "---<span style='color: blueviolet;'>".$fsize."</span>";
					}
				}elseif($ext=="GB"){
					if($num>$gbsize){
						$arrgb[]=$path."/".$v."---<span style='color: red;'>".$fsize."</span>";
						echo "---<span style='font-size: 48px; color: red;'>".$fsize."</span>";
					}else{
						echo "---<span style='color: red;'>".$fsize."</span>";
					}
				}
				echo "<br>";
			}
		}
	}		
}
$dirname='';
$bsize= 1000;
$kbsize= 1000;
$mbsize= 5;
$gbsize= 1;
if (!empty($_POST['webname'])) {
	$dirname = $_POST['webname'];
	$bsize= $_POST['b'];
	$kbsize= $_POST['kb'];
	$mbsize= $_POST['mb'];
	$gbsize= $_POST['gb'];
	echo $dirname;
	echo "<hr>";
	getdir($dirname,$bsize,$kbsize,$mbsize,$gbsize);

	echo "<hr><hr>";
	echo "递归".$dirname . "</br>";
	echo "本次递归统计：递归文件夹" . $dir . "个！" ;
	echo "<br>";
	echo "遍历的文件总计:" . $files . "个!";
	echo "<hr>";
	
	echo "【B级别】大于".$bsize."B的文件有：";
	echo "<pre>";
	print_r($arrb);
	echo "</pre>";
	echo "<hr>";
	
	echo "【KB级别】大于".$kbsize."KB的文件有：";
	echo "<pre>";
	print_r($arrkb);
	echo "</pre>";
	echo "<hr>";
	
	echo "【MB级别】大于".$mbsize."MB的文件有：";
	echo "<pre>";
	print_r($arrmb);
	echo "</pre>";
	echo "<hr>";
	
	echo "【GB级别】大于".$gbsize."GB的文件有：";
	echo "<pre>";
	print_r($arrgb);
	echo "</pre>";
	echo "<hr>";
	
}
if (!empty($_POST['webname'])) {
	echo "<hr>";
	echo "****************************************--结束--******************************************";
	echo "<hr>";
}
?>
<html>
	<head>
		<meta charset="utf-8">
		<title>递归文件大小</title>
	</head>
	<body>
		<form action="#" method="post">
			<br />
			<br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入要递归的文件夹：
			<input type="text" name="webname" id="webname" value="<?php echo $dirname; ?>" /> <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入B级别大小限制：&nbsp;
			<input type="text" name="b" value="<?php echo $bsize; ?>" />B <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入KB级别大小限制：
			<input type="text" name="kb" value="<?php echo $kbsize; ?>" />KB <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入MB级别大小限制：
			<input type="text" name="mb" value="<?php echo $mbsize; ?>" />MB <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入GB级别大小限制：
			<input type="text" name="gb" value="<?php echo $gbsize; ?>" />GB <br />
			<input style="margin-left: 300px;margin-top: 12px;" type="submit" value="开始递归"/>
		</form>
		<br />
		<br />
		<h3>注：输入本程序文件的相对目标文件夹</h3>
		<br />
		<br />
	</body>
</html>